package com.CampaignManager.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class City {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	@Persistent
	private String name;
	@Persistent
	private double latitude;
	@Persistent
	private double longitude;
	@Persistent
	private String faction;
	
	@Persistent(mappedBy = "city")
    private List<CityRelate> relations;	
	public List<CityRelate> getRelations() {
		if (relations == null) relations = new ArrayList<CityRelate>();
		return relations;
	}
	public CityRelate getRelation(String otherCity) {
		CityRelate rel = null;
		for (CityRelate pc : this.getRelations()) {
			if (pc.getOtherCity().equalsIgnoreCase(otherCity)) {
				rel = pc;
			}
		}
		if (rel == null) {
			rel = new CityRelate();
			rel.setOwnerCity(this.getName());
			rel.setOtherCity(otherCity);
			rel.setValue(0L);
			this.getRelations().add(rel);
		}
		return rel;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getLatitude() {
		return latitude;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	public double getLongitude() {
		return longitude;
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	public String getFaction() {
		return faction;
	}
	public void setFaction(String faction) {
		this.faction = faction;
	}
	public Long getId() {
		return id;
	}	
}
